﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;

#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(TestTabletInput))]
public class TestTabletInputEditor : E_ShowButtons<TestTabletInput> { }
#endif

public enum MouseMessages
{
    WM_LBUTTONDOWN = 0x0201,
    WM_LBUTTONUP = 0x0202,
    WM_MOUSEMOVE = 0x0200,
    WM_MOUSEWHEEL = 0x020A,
    WM_RBUTTONDOWN = 0x0204,
    WM_RBUTTONUP = 0x0205
}
public class WinAPI
{
    [DllImport("user32.dll")]//, SetLastError = true)]
    public static extern bool PeekMessage(out MSG lpMsg,
                                    Int32 hwnd,
                                    Int32 wMsgFilterMin,
                                    Int32 wMsgFilterMax,
                                    Int32 wRemoveMsg);
    public const UInt32 WM_QUIT = 0x0012;
    public const Int32 PM_REMOVE = 1;
    [StructLayout(LayoutKind.Sequential)]
    [Serializable]
    public struct POINTAPI
    {
        public Int32 x;
        public Int32 y;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct MSG
    {
        public IntPtr hwnd;
        public Int32 message;
        public IntPtr wParam;
        public IntPtr lParam;
        public Int32 time;
        public POINTAPI pt;
        public override string ToString()
        {
            return hwnd + ", " + message + ", " + wParam;// + ", " + lParam + ", " + time + ", " + pt;
        }
    }
    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out POINT pt);
    [StructLayout(LayoutKind.Sequential)]
    [Serializable]
    public struct POINT
    {
        public int x;
        public int y;
    }
    [StructLayout(LayoutKind.Sequential)]
    [Serializable]
    public struct Point
    {
        public int x;
        public int y;
        public override string ToString()
        {
            return x.ToString() + y.ToString();
        }
    }
}
public class TestTabletInput : MonoBehaviour
{

    NativeMessage nm;
    public bool log;

    public int handle;
    public uint msg;
    public int wParam;
    public int lParam;
    public uint time;
    public WinAPI.Point p;

    [StructLayout(LayoutKind.Sequential)]
    [Serializable]
    public struct NativeMessage
    {
        public IntPtr handle;
        public uint msg;
        public IntPtr wParam;
        public IntPtr lParam;
        public uint time;
        public WinAPI.Point p;
        //public override string ToString()
        //{
        //    return handle + ", " + msg + ", " + wParam + ", " + lParam + ", " + time + ", " + p;
        //}
        //public override string ToString()
        //{
        //    return "handle: " + handle + ",msg: " + msg + ",wP " +
        //        wParam + ",lP " + lParam + ", " + p + Environment.NewLine;
        //}
        public override string ToString()
        {
            string str = "";
#if !UNITY_EDITOR
            str += "msg: " + msg;
#endif
            str += ",wP " + wParam.ToString().Substring(0,
                Mathf.Min(wParam.ToString().Length, 3))
                + ", " + Environment.NewLine;

            return str;
        }
    }

    [DllImport("user32.dll")]
    public static extern int PeekMessage(out NativeMessage lpMsg, IntPtr window, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg);

    const uint PM_QS_INPUT = (QS_INPUT << 16);
    const uint QS_INPUT = (QS_MOUSE | QS_KEY | QS_RAWINPUT);
    const uint QS_MOUSE = (QS_MOUSEMOVE | QS_MOUSEBUTTON);
    const uint QS_MOUSEMOVE = 0x0002;
    const uint QS_KEY = 0x0001;
    const uint QS_RAWINPUT = 0x0400;
    const uint QS_MOUSEBUTTON = 0x0004;
    public Transform react;
    void Update()
    {
        var a = new NativeMessage();
        int c = PeekMessage(out a, IntPtr.Zero, 0, 0, PM_QS_INPUT);
        nm = a;
        if (c != 0)
        {
            handle = a.handle.ToInt32();
            msg = a.msg;
            wParam = a.wParam.ToInt32();
            lParam = a.lParam.ToInt32();
            time = a.time;
            p = a.p;
            //Debug.Log(((IntPtr)MouseMessages.WM_LBUTTONDOWN).ToInt32());
            if (a.msg == (uint)MouseMessages.WM_LBUTTONDOWN ||
                a.wParam == (IntPtr)MouseMessages.WM_LBUTTONDOWN ||
                a.lParam == (IntPtr)MouseMessages.WM_LBUTTONDOWN)
            {
                Debug.LogError("mousedown");
            }
            else if (a.wParam != (IntPtr)0)
            {
                //Debug.LogError(a.ToString()); // prints strings
                var s = wParam.ToString().Substring(0,
                Mathf.Min(wParam.ToString().Length, 3));
                if (s == "538")
                    react.localScale = Vector3.one * 2f;
                else if (s == "539")
                    react.localScale = Vector3.one * 3f;
                else
                    react.localScale = Vector3.one * 1.5f;
                return;
            }
        }
        if (react.localScale.x > 1)
        {
            react.localScale = Vector3.one * (react.localScale.x - 0.01f);
        }
    }
}

